package com.example.javaweb;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

@WebServlet("/ListCakeServlet")
public class ListCakeServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        Collection<Cake> cakes = CakeDB.getAll();
        PrintWriter writer = resp.getWriter();
        for (Cake cake : cakes) {
            //获取蛋糕id
            String id = cake.getId();
            //获取蛋糕name
            String name = cake.getName();
            String url="PurchaseServlet?id="+id;
            //输出
            writer.write(id+"  "+name+"<a href='"+url+"'>点击购买</a>"+"<br/>");
        }
    }
}
